package cn.edu.uestc.controller;

import cn.edu.uestc.pojo.Logistics;
import cn.edu.uestc.pojo.Order;
import cn.edu.uestc.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class OrderController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    OrderService orderService;

    @PostMapping("add_order")
    public boolean addOrder(int id, String shopName, String productName, String receiver, String receiverAddress,
                            String receiverPhone) {
        Order order = new Order();
        order.setId(id);
        order.setUsername((String) request.getSession().getAttribute("username"));
        order.setShopName(shopName);
        order.setProductName(productName);
        order.setStatus("已下单");
        order.setOrderTime(new Date());
        order.setReceiver(receiver);
        order.setReceiverAddress(receiverAddress);
        order.setReceiverPhone(Long.parseLong(receiverPhone));

        return orderService.addOrder(order);
    }

    @PostMapping("update_order")
    public void updateOrder(int id, String address, String operator, boolean isEnd) {
        orderService.updateOrder(id, address, operator, isEnd);
    }

    @GetMapping("get_order_by_id")
    public Order getOrderById(int id) {
        System.out.println("id = " + id);
        Order order = orderService.getOrderById(id);
        System.out.println(order.toString());
        return order;
    }

    @GetMapping("delete_order_by_id")
    public String deleteById(int id)
    {
        orderService.deleteOrderById(id);
        return "成功";
    }

    @GetMapping("get_all_orders")
    public Map<String,Object> getAllOrders()
    {
        Map<String,Object> map = new HashMap<>();
        List<Order> orderList = orderService.getAllOrders();
        map.put("code", "0");
        map.put("count", orderList.size());
        map.put("data", orderList);
        return map;
    }

    @GetMapping("get_all_shops")
    public List<String> getAllShops() {
        return orderService.getAllShops();
    }

    @PostMapping("accept_order")
    public void acceptOrder(int id, String shipper, String shippingAddress, String shipperPhone, String operator) {
        orderService.acceptOrder(id, shipper, shippingAddress, shipperPhone, operator);
    }

    @GetMapping("finish_order")
    public void finishOrder(int id) {
        orderService.finishOrder(id);
    }
}
